<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="名称" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sen:user:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sen:user:remove']"
        >删除</el-button>
      </el-col>
       <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          :disabled="multiple"
          @click="luruyuan"
          v-hasPermi="['sen:user:setluru']"
        >设置为录入员</el-button>
      </el-col>
        <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          :disabled="multiple"
          @click="guanliyuan"
          v-hasPermi="['sen:user:admin']"
        >设置为管理员</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="账号" align="center" prop="username" />
      <el-table-column label="昵称" align="center" prop="nc" />
      <el-table-column label="姓名" align="center" prop="xm" />
      <el-table-column label="街道-居委" align="center" prop="pwd" />
      <el-table-column label="手机" align="center" prop="dttime" />
      <el-table-column label="是否是录入员" align="center" prop="isluru" >
        <template slot-scope="scope">
          <span>{{scope.row.isluru?"是":"否"}}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否是管理员" align="center" prop="isadmin" >
         <template slot-scope="scope">
          <span>{{scope.row.isadmin?"是":"否"}}</span>
        </template>
      </el-table-column>
      <el-table-column label="注册时间" align="center" prop="lasttime" >
          <template slot-scope="scope">
          <span>{{ timestampToTime(scope.row.dtTime) }}</span>
        </template>
        </el-table-column>>
      <el-table-column label="最后登录" align="center" prop="lasttime" >
          <template slot-scope="scope">
          <span>{{ timestampToTime(scope.row.lasttime) }}</span>
        </template>
        </el-table-column>>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input v-model="form.pwd" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="姓名" prop="xm">
          <el-input v-model="form.xm" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser, exportUser,setLuruYuan,setAdmin } from "@/api/assess/user";

export default {
  name: "User",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        username: null,
        bid: null,
        email: null,
        pwd: null,
        dttime: null,
        ip: null,
        groupid: null,
        istrue: null,
        lasttime: null,
        lognum: null,
        status: null,
        type: null,
        openid: null,
        oopenid: null,
        expirationTime: null,
        currentStatus: null,
        isdisable: null,
        renewtype: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        pwd: [
          { required: true, message: "原op不能为空", trigger: "blur" }
        ],
        dttime: [
          { required: true, message: "原op不能为空", trigger: "blur" }
        ],
        ip: [
          { required: true, message: "原op不能为空", trigger: "blur" }
        ],
        groupid: [
          { required: true, message: "原op不能为空", trigger: "blur" }
        ],
        istrue: [
          { required: true, message: "原op不能为空", trigger: "blur" }
        ],
        expirationTime: [
          { required: true, message: "原op不能为空", trigger: "blur" }
        ],
        currentStatus: [
          { required: true, message: "原op不能为空", trigger: "blur" }
        ],
        isdisable: [
          { required: true, message: "原op不能为空", trigger: "blur" }
        ],
        renewtype: [
          { required: true, message: "原op不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
      timestampToTime (timestamp) {
      if(timestamp == null || timestamp == '' || timestamp== 0){
        return;
      }
       // 计算年月日时分的函数
       var date = new Date(timestamp*1000)
       var Y = date.getFullYear() + '-'
       var M = (date.getMonth() + 1) + '-'
       var D = date.getDate() + ' '
       var h = date.getHours() + ':'
       var m = date.getMinutes() + ':'
       var s = date.getSeconds()
       return Y + M + D + h + m + s
   },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        username: null,
        bid: null,
        email: null,
        pwd: null,
        dttime: null,
        ip: null,
        groupid: null,
        istrue: null,
        lasttime: null,
        lognum: null,
        status: "0",
        type: null,
        openid: null,
        oopenid: null,
        expirationTime: null,
        currentStatus: 0,
        isdisable: null,
        renewtype: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【请填写功能名称】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项？').then(function() {
        return delUser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
     luruyuan(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认设置/取消用户为录入员？').then(function() {
        return setLuruYuan(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("设置成功");
      }).catch(() => {});
    },
     guanliyuan(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认设置/取消用户为管理员？').then(function() {
        return setAdmin(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("设置成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有【请填写功能名称】数据项？').then(() => {
        this.exportLoading = true;
        return exportUser(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script> 


<style  scoped>
 ::v-deep .el-form-item--mini.el-form-item{
    margin-bottom: 10px !important;
    
  }
  ::v-deep .el-form-item__label{
    /* font-size: 20px !important; */
  }
</style>